package com.uchedao.seller.ui.car.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.baidu.mobstat.StatService;
import com.uchedao.seller.http.VolleyUtil;
import com.uchedao.seller.ui.user.LoginFragment;
import com.uchedao.ulibrary.fragment.CommonActivity;

/**
 * Created by hang on 2015/10/23.
 */
public abstract class TabBaseFragment extends Fragment {

    /**
     * 当前Fragment顶级视图
     */
    public View mRoot;

    /**
     * 当前软件Context
     */
    protected Context mContext;

    /**
     * 页面标签
     */
    protected String TAG = "BaseFragment";

    protected RequestQueue mQueue;

    /**
     * 设置页面标签
     */
    protected abstract String getActivityTag();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity().getApplicationContext();
        TAG = getActivityTag();
        //获取单例网络请求队列
        mQueue = VolleyUtil.getInstance().getRequestQueue();
        // 调试百度统计SDK的Log开关，可以在Eclipse中看到sdk打印的日志，发布时去除调用，或者设置为false
        StatService.setDebugOn(false);
    }

    /**
     * 网络请求添加到请求队列中
     */
    public void addQueue(Request queue) {
        //设置标签
        queue.setTag(TAG);
        if (mQueue != null) {
            mQueue.add(queue);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        StatService.onPageEnd(mContext, TAG);
        //取消当前页面所有的网络请求
        if (mQueue != null) {
            mQueue.cancelAll(TAG);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        StatService.onPageStart(mContext, TAG);
    }

    protected Context getContext() {
        return mContext;
    }

    protected Context getApplicationContext() {
        return mContext;
    }

    /**
     * 提示
     */
    public void showToast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 提示
     */
    public void showToast(int resString) {
        if (isAdded()) {
            showToast(getResources().getString(resString));
        }
    }

    /**
     * 进入登录界面
     * */
    public void toLogin() {
        toFragment(LoginFragment.getInstance(), true, true);
    }

    /**
     * 前往
     *
     * @param fragment 目标视图
     * @param canBack  是否可返回
     */
    public final void toFragment(Fragment fragment, boolean canBack) {
        ((CommonActivity) getActivity()).toFragment(fragment, canBack);
    }

    /**
     * 前往
     *
     * @param fragment 目标视图
     * @param canBack  是否可返回
     * @param anim     是否执行动画
     */
    public final void toFragment(Fragment fragment, boolean canBack,
                                 boolean anim) {
        ((CommonActivity) getActivity()).toFragment(fragment, canBack, anim);
    }

    public boolean onBackPress() {
        return false;
    }

    ;


    /**
     * 获取Dialog 展示状态
     */
    public boolean isProgressDialogShowing() {
        return ((CommonActivity) getActivity()).isProgressDialogShowing();
    }

    /**
     * 创建并展示Dialog
     */
    public void showProgressDialog(String msg) {
        ((CommonActivity) getActivity()).showProgressDialog(msg);
    }

    ;

    /**
     * 关闭Dialog
     */
    public void cancelProgressDialog() {
        ((CommonActivity) getActivity()).cancelProgressDialog();
    }

    ;
}
